package com.cooldatasoft.app;

import com.vaadin.Application;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.incubator.dragdroplayouts.DDAbsoluteLayout;
import com.vaadin.incubator.dragdroplayouts.DDAbsoluteLayout.AbsoluteLayoutTargetDetails;
import com.vaadin.incubator.dragdroplayouts.client.ui.LayoutDragMode;
import com.vaadin.incubator.dragdroplayouts.events.LayoutBoundTransferable;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.AbsoluteLayout.ComponentPosition;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.Window;

public class VaadinMavenWebApplication extends Application {
	@Override
	public void init() {

		Window main = new Window("Hello window");
		setMainWindow(main);
		main.setSizeFull();
		main.addComponent(createAbsoluteLayoutDemo());

	}

	private Component createAbsoluteLayoutDemo() {

		final DDAbsoluteLayout layout = new DDAbsoluteLayout();
		layout.setStyleName("blue");
		layout.setHeight("400px");
		layout.setWidth("400px");
		layout.setCaption("Absolute Layout");

		// Enable dragging components
		layout.setDragMode(LayoutDragMode.CLONE);

		// Enable dropping components
		layout.setDropHandler(new DropHandler() {

			@Override
			public AcceptCriterion getAcceptCriterion() {
				return AcceptAll.get();
			}

			@Override
			public void drop(DragAndDropEvent event) {
				AbsoluteLayoutTargetDetails details = (AbsoluteLayoutTargetDetails) event.getTargetDetails();
				int left = details.getRelativeLeft();
				int top = details.getRelativeTop();

				LayoutBoundTransferable trans = (LayoutBoundTransferable) event.getTransferable();

				Component c = trans.getComponent();
				ComponentPosition p = layout.getPosition(c);

				p.setLeft((float) left, Sizeable.UNITS_PIXELS);
				p.setTop((float) top, Sizeable.UNITS_PIXELS);

			}
		});

		// Add some components
		Label lbl = new Label("This is an Absolute layout, you can freely drag the components around");
		layout.addComponent(lbl);
		Button btn = new Button("Button 1", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				getMainWindow().showNotification("Click!");
			}
		});
		layout.addComponent(btn, "left:50px; top:50px");
		Link link = new Link("A link to Vaadin", new ExternalResource("http://www.vaadin.com"));
		layout.addComponent(link, "left:200px; top:100px");

		return layout;
	}
}
